package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional

public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    //增
    @Override
    public void addPermission(Permission permission) {
        permissionDao.addPermission(permission);
    }

    //删
    @Override
    public void deletePermissionById(Integer id) {
        //检查是否有关联,如果有关联,不允许删除
        long count=permissionDao.findCountByPermissionId(id);
        if(count>0){
            new RuntimeException();
        }

        //执行删除
       permissionDao.deletePermissionById(id);
    }

    //改
    @Override
    public void editPermissionByCondition(Permission permission) {
       permissionDao.editPermissionByCondition(permission);
    }

    //根据条件查询
    @Override
    public Permission findByCondition(Permission permission) {
        return  permissionDao.findByCondition(permission);
    }

    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Permission> page=permissionDao.findByPermissionName(queryPageBean.getQueryString());
        long total = page.getTotal();
        List<Permission> row = page.getResult();
        return new PageResult(total,row);
    }

    //查询所有
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
